<template>
  <AssetAssignList
    ref="refUserList"
    :tag="'用户'"
    :name="props.entry.name"
    :assign-columns="assignColumns"
    :columns="columns"
    :load-assets="loadRealmUsers"
    :assign-assets="assignRealmUsers"
    :load-assign-assets="loadAssignRealmUsers"
    :level-mode="false"
  >
    <template v-slot:comment>
      <slot name="comment" />
    </template>
  </AssetAssignList>
</template>

<script setup lang="ts">
import {ref} from 'vue'
import {useLoadAssign} from '@/utils/sop/UseLoadAssign'
import AssetAssignList from '@/views/sop/assign/AssetAssignList.vue'
import {TYPES} from "@/stores/conf";
import {msgError, msgInfo} from "@/utils/InfoUtil";

const refUserList = ref();

const props = withDefaults(
  defineProps<{
    entry: any,
    loadUser?: any,
    assignUser?: any,
    loadAssignUser?: any,
    removeUser?: any,
  }>(),
  {}
)

const { current, load, assign, loadAssign, removeHolderOfAsset} = useLoadAssign(props.entry)

const assignColumns = ref([
  {
    id: 'id',
    name: '标识',
    hide: 'true'
  },
  {
    id: 'name',
    name: '用户名称'
  },
  {
    id: 'phone',
    name: '电话号码'
  }
])

const columns = ref([
  {
    id: 'id',
    name: '标识',
    hideCol: true,
    show: false
  },
  {
    id: 'name',
    name: '用户名称',
    show: true
  },
  {
    id: 'phone',
    name: '电话号码',
    show: true
  },
  {
    id: 'state',
    name: '状态',
    show: true,
    tag: 'true',
    type: 'select',
    tagType: (tag: number) => {
      return tag === 1 ? 'success' : 'danger'
    },
    tagText: (tag: number) => {
      return tag === 0 ? '禁用' : '启用'
    },
    options: [
      {
        value: 1,
        label: '启用'
      },
      {
        value: 2,
        label: '禁用'
      }
    ]
  },
  {
    id: 'createTime',
    name: '创建时间',
    show: true
  },
  {
    id: 'operation',
    name: '操作',
    show: true,
    canEdit: false,
    hideCol: true,
    fixed: 'right',
    operations: [
      {
        type: 'confirm',
        name: '移除',
        tips: () => { return '确认移除？' },
        confirm: async (data: any, row: any, index: number) => {
          if (props.removeUser) {
            await props.removeUser(row);
          } else {
            await removeHolderOfAsset(getTargetId(), TYPES.USER, props.entry, row);
          }
          refresh();
        },
        cancel: (data: any, index: number) => {
          const row = data[index];
        }
      }
    ]
  }
])

const getTargetId = () => {
  return '';
}

/**
 * 分页读取分配给当前角色的权限列表
 */
const loadRealmUsers = async (params: any) => {
  if (props.loadUser) {
    return await props.loadUser(params);
  }
  return await load(getTargetId(), TYPES.USER, params)
}

/**
 * 为角色分配权限
 */
const assignRealmUsers = async (items: any) => {
  if (props.assignUser) {
    return await props.assignUser(items);
  }
  return await assign(getTargetId(), TYPES.USER, items)
}

/**
 * 分页读取可以分配给角色的权限列表
 */
const loadAssignRealmUsers = async (params: any) => {
  if (props.loadAssignUser) {
    return await props.loadAssignUser(params);
  } else {
    return await loadAssign(getTargetId(), TYPES.USER, params)
  }
}

const refresh = () => {
  refUserList.value?.refresh();
}

defineExpose({
  refresh,
})

</script>

<style scoped>
.el-form-item-class .el-form-item__label {
  font-size: 12px;
}

.permit-assign-btn {
  margin: 16px 0;
}
</style>
